/*
 * =====================================================================================
 *
 *       Filename:  packet_capture.h
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  2025年03月17日 18时38分36秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  xiaojian jiang (Adams), <yunsangc@163.com>
 *   Organization:  
 *
 * =====================================================================================
 */

#ifndef __PACKET_CAPTURE_H__
#define __PACKET_CAPTURE_H__

#include <pcap.h>
#include <mosquitto.h>
#include "list.h"

/* PCAP_OPENFLAG_PROMISCUOUS is 0x00000001 在libpcap 1.5之后才被添加 */
#ifndef PCAP_OPENFLAG_PROMISCUOUS
#define PCAP_OPENFLAG_PROMISCUOUS 0x00000001
#endif

#define NEW_YES 1
#define NEW_NO 0

typedef unsigned char u_char;
typedef void (*publish_cb_t)(struct mosquitto *, void *, int );
typedef enum {
	IDEL = 0,
	FILE_OPENNING,
	FILE_RADY,
	FILE_WRITING,
	FILE_WRIT_END
} PCAP_Run_state;

typedef struct pcap_file_struct {
	struct list_head node;
	char *file_path;
	char *file_path_tmp;
	pcap_dumper_t *file;
	uint64_t count;
	uint64_t size;
} pcap_file_t;

typedef struct struct_packet_capture
{
	pcap_t *handler;
	pcap_dumper_t *file;
	int change_file;//是否在修改目标文件
	char *write_dir; //文件集合目录
	int vlanid; //设定的VLAN ID
	char *wid; //任务ID
	char *task_dir; //任务目标目录($write_dir/wid)
	char *file_path;//当前正在输入的文件绝对路径
	char *file_path_tmp;
	time_t start_write_time; //本次任务开始时间戳
	struct list_head files_list; //输出文件分段链表
	uint32_t files_count; //分段文件数量

	/* MQTT */
	struct mosquitto *mosq;
	publish_cb_t mqtt_publish;
} packet_capture_t;

int pcap_file_write_lock_init();
packet_capture_t *get_pkgcap_ctx(); // 获取packet_capture_t *
void set_single_file_max_size(int _size_MB);
packet_capture_t *new_packet_capture(const char *net_device); // 创建packet_capture_t *
void destroy_packet_capture(packet_capture_t *_del); // 删除和释放packet_capture_t *

void packet_handler_cb(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data); // pcap抓包callback
pcap_t *open_netdevice_package_capture(const char *net_device); // pcap打开网络设备
void control_start_stop_write_file(const char *commond_str, packet_capture_t *pkgcap_ctx); // 通过JSON string修改抓包动作

#endif //"__PACKET_CAPTURE_H__"
